package com.graduation.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.graduation.common.enums.DocTypeEnum;
import com.graduation.pojo.po.*;
import com.graduation.pojo.vo.DocumentTypeChartVO;
import com.graduation.pojo.vo.SearchTrendVO;
import com.graduation.pojo.vo.StatisticsVO;
import com.graduation.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class StatisticsServiceImpl implements StatisticsService {

    private final UserService userService;
    private final LegalDocumentService legalDocumentService;
    private final CaseService caseService;
    private final CommentService commentService;
    private final FavoriteService favoriteService;
    private final SearchHistoryService searchHistoryService;

    @Override
    public StatisticsVO getBasicStatistics() {
        StatisticsVO statistics = new StatisticsVO();
        
        // 总数统计
        statistics.setTotalUsers(userService.count());
        statistics.setTotalDocuments(legalDocumentService.count());
        statistics.setTotalCases(caseService.count());
        statistics.setTotalComments(commentService.count());
        statistics.setTotalFavorites(favoriteService.count());
        
        // 今日统计
        LocalDateTime todayStart = LocalDate.now().atStartOfDay();
        LocalDateTime todayEnd = LocalDate.now().atTime(LocalTime.MAX);
        
        // 今日新增用户
        long todayNewUsers = userService.count(new LambdaQueryWrapper<User>()
                .between(User::getCreateTime, todayStart, todayEnd));
        statistics.setTodayNewUsers(todayNewUsers);
        
        // 今日新增文档
        long todayNewDocuments = legalDocumentService.count(new LambdaQueryWrapper<LegalDocument>()
                .between(LegalDocument::getCreateTime, todayStart, todayEnd));
        statistics.setTodayNewDocuments(todayNewDocuments);
        
        // 今日新增评论
        long todayNewComments = commentService.count(new LambdaQueryWrapper<Comment>()
                .between(Comment::getCreateTime, todayStart, todayEnd));
        statistics.setTodayNewComments(todayNewComments);
        
        return statistics;
    }

    @Override
    public SearchTrendVO getSearchTrend() {
        SearchTrendVO trendVO = new SearchTrendVO();
        List<String> dates = new ArrayList<>();
        List<Long> counts = new ArrayList<>();
        
        LocalDate endDate = LocalDate.now();
        int days = 7; // 固定统计最近7天
        
        for (int i = days - 1; i >= 0; i--) {
            LocalDate date = endDate.minusDays(i);
            LocalDateTime dayStart = date.atStartOfDay();
            LocalDateTime dayEnd = date.atTime(LocalTime.MAX);
            
            // 统计该日期的搜索次数
            long count = searchHistoryService.count(new LambdaQueryWrapper<SearchHistory>()
                    .between(SearchHistory::getSearchTime, dayStart, dayEnd));
            
            dates.add(DateUtil.format(DateUtil.date(date), "MM-dd"));
            counts.add(count);
        }
        
        trendVO.setDates(dates);
        trendVO.setCounts(counts);
        
        return trendVO;
    }

    @Override
    public DocumentTypeChartVO getDocumentTypeChart() {
        DocumentTypeChartVO chartVO = new DocumentTypeChartVO();
        List<DocumentTypeChartVO.DocumentTypeItem> items = new ArrayList<>();
        
        // 统计各种文档类型的数量
        for (DocTypeEnum docType : DocTypeEnum.values()) {
            long count = legalDocumentService.count(new LambdaQueryWrapper<LegalDocument>()
                    .eq(LegalDocument::getDocType, docType));
            
            if (count > 0) {
                items.add(new DocumentTypeChartVO.DocumentTypeItem(docType.getDesc(), count));
            }
        }
        
        chartVO.setItems(items);
        
        return chartVO;
    }
}
